<template>
  <div class="user-avatar-dropdown">
    <Dropdown @on-click="handleClick">
      <Badge>
        <Avatar :src="userAvatar" />
      </Badge>
      <Icon :size="18" type="md-arrow-dropdown"></Icon>
      <DropdownMenu slot="list">
        <DropdownItem name="message">消息中心</DropdownItem>
        <DropdownItem name="modifyPwd">修改密码</DropdownItem>
        <DropdownItem name="logout">退出登录</DropdownItem>
      </DropdownMenu>
    </Dropdown>
    <Modal v-model="pwdvisible" title="修改密码" :mask-closable="false" @on-ok="handleModifyPwd" @on-cancel="handleCancelPwd">
      <Row class="row" style="margin-top:0;">
        <Col span="6">
        <strong class="title">新密码：</strong>
        </Col>
        <Col span="18">
        <Input type="password" v-model="password" placeholder="请输入新密码" class="form" />
        </Col>
      </Row>
      <Row class="row" style="margin-top:20;">
        <Col span="6">
        <strong class="title">确认密码：</strong>
        </Col>
        <Col span="18">
        <Input type="password" v-model="repeatPassword" placeholder="请输入确认密码" class="form" />
        </Col>
      </Row>
    </Modal>
  </div>
</template>

<script>
import './user.less'
import { mapActions } from 'vuex'
import jsCookie from 'js-cookie'
import rcRequest from 'rc-request'

export default {
  name: 'User',
  props: {
    userAvatar: {
      type: String,
      default: ''
    },
    messageUnreadCount: {
      type: Number,
      default: 0
    },
  },
  data() {
    return {
      pwdvisible: false,
      password: '',
      repeatPassword: '',
    };
  },
  methods: {
    ...mapActions([
      'handleLogOut'
    ]),
    handleCancelPwd() {
      this.pwdvisible = false;
      this.password = '';
      this.repeatPassword = '';
    },
    async handleModifyPwd() {
      if (this.password.length === 0) {
        this.$Message.error('新密码不能为空');
        return false;
      }

      if (this.repeatPassword.leng === 0) {
        this.$Message.error('确认密码不能为空');
        return false;
      }

      if (this.password.length < 6 || this.repeatPassword.length < 6) {
        this.$Message.error('密码不能少于6位');
        return false;
      }

      if (this.password !== this.repeatPassword) {
        this.$Message.error('新密码和确认密码不一致');
        return false;
      }

      await rcRequest({
        method: 'post',
        url: '/login/update_login_user_info',
        data: { password: this.password },
      });

      this.password = '';
      this.repeatPassword = '';

      this.$Message.success('密码修改成功');
    },
    modifyPwd() {
      this.pwdvisible = true;
    },
    async logout() {
      await rcRequest({
        method: 'post',
        url: '/login/exit_login_user_info',
        data: {},
      })
      jsCookie.remove('user_id')
      jsCookie.remove('agent_id')
      await this.handleLogOut()
      this.$router.push({
        name: 'login'
      })
    },
    message() {
      this.$router.push({
        name: 'message_page'
      })
    },
    handleClick(name) {
      switch (name) {
        case 'logout': this.logout()
          break
        case 'message': this.message()
          break
        case 'modifyPwd': this.modifyPwd()
          break
      }
    }
  }
}
</script>

<style scoped>
.row {
  margin-top: 20px;
  display: flex;
  align-items: center;
}

.title {
  display: flex;
  justify-content: flex-end;
  padding-right: 20px;
}

.form {
  width: 85%;
}
</style>